package com.epam.rentcar.controller.actions;

import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.epam.rentcar.model.CarRent;

public class RequestHandlerAction implements Action {

	private static Logger log = Logger.getLogger(RequestHandlerAction.class);
	
	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {

		String name = request.getParameter("name");
		String passport = request.getParameter("passport");

		int carID = Integer.parseInt(request.getParameter("carID"));
		int rentDuration = Integer.parseInt(request.getParameter("rentDuration"));

		CarRent carRentAppModel = (CarRent) request.getSession().getAttribute("carRentAppModel");

		try {
			carRentAppModel.addRequest(name, passport, carID, rentDuration);
			carRentAppModel.makeCarUnavaliable(carID);

			request.setAttribute("message", "you successfully left a car request");

		} catch (SQLException | NamingException e) {
			log.error(e);
			throw new ServletException(e);
		} 

		return "/message.jsp";

	}	
}
